package com.kzj.laimeimei.activity;

import android.os.Bundle;
import android.view.View;

import com.kzj.laimeimei.R;
import com.kzj.laimeimei.beans.OrderBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.EvaluationOrderCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class EvaluationOrderActivity extends GBaseActivity {
    private MaterialListView material_listview;
    private EvaluationOrderCard evaluationOrderCard;
    private OrderBean orderBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
    }

    @Override
    protected void initDatas() {
        setTopTitle("评价订单");
        setLeftImg(View.VISIBLE, R.drawable.back);
        setRightText(View.VISIBLE, "发送");
        orderBean = (OrderBean) getIntent().getSerializableExtra("order_bean");
    }

    @Override
    protected void fillArrayInListView() {
        evaluationOrderCard = new EvaluationOrderCard(this);
        evaluationOrderCard.setOrderBean(orderBean);
        material_listview.add(evaluationOrderCard);
    }

    private void TaskEvaluation(String order_id, String goods, String store, String content) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Evaluation, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Evaluation, order_id, goods, store, content), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        App.getInstance().showToast(result.getMsg().toString());
                        EventBus.getDefault().post(orderBean);
                        EvaluationOrderActivity.this.finish();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    @Override
    public void doClickRightText() {
        super.doClickRightText();
        int storeNum = 0;
        int projectNum = 0;
        for (int i = 0; i < evaluationOrderCard.getProject().length; i++) {
            if (evaluationOrderCard.getProject()[i])
                projectNum++;
        }
        for (int i = 0; i < evaluationOrderCard.getStore().length; i++) {
            if (evaluationOrderCard.getStore()[i])
                storeNum++;
        }
        TaskEvaluation(orderBean.getOrder_id(), projectNum + "", storeNum + "", evaluationOrderCard.getContent());
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
